package com.atguigu.chaptar6

object $06_WordCountHight {
  //高级wordcount案例
  def main(args: Array[String]): Unit = {
    val tupleList = List( ("Hello Scala Spark World", 4) , ("Hello Scala Spark", 3), ("Hello Scala", 2), ("Hello", 1))
    //1.切分,扁平化
    val tuples = tupleList.flatMap((x) => {
      val array = x._1.split(" ") //Array(hello,scala,spark,world)
      array.map(y => (y, x._2))})//List((Hello,4), (Scala,4), (Spark,4), (World,4), (Hello,3), (Scala,3), (Spark,3), (Hello,2), (Scala,2), (Hello,1))
    //2.groupBy
    val map = tuples.groupBy(x => {
      x._1
    })//Map(Hello -> List((Hello,4), (Hello,3), (Hello,2), (Hello,1)), Spark -> List((Spark,4), (Spark,3)), Scala -> List((Scala,4), (Scala,3), (Scala,2)), World -> List((World,4)))
    //3.map统计
    val iterable = map.map((x) => {
      val list = x._2
      val value = list.map(_._2)
      (x._1,value.sum)//Map(Hello -> 10, Spark -> 7, Scala -> 9, World -> 4)
    })
    //4.结果展示
    iterable.foreach(println(_))


    //一气呵成
  }

}
